#include "common.h"

#include "module_client.h"

module_client::module_client(void)
{
	strcpy_s(IP, "10.0.0.55\0");
	memset(&server, 0, sizeof(SOCKADDR_IN)); 
	server.sin_family = AF_INET;
	server.sin_port = htons(3333);
	server.sin_addr.s_addr = inet_addr(IP);
	memset(&UID, 0, 16); 

	state = 0;
}

module_client::~module_client(void)
{
}


VOID module_client::MakeHttpHead(CHAR * Buffer, CHAR * Path, CHAR * content) {
	Buffer[0] = '\0';
	strcat(Buffer,"POST ");
	strcat(Buffer, Path);
	strcat(Buffer," HTTP/1.1\r\nAccept: */*\r\nCache-Control: no-cache\r\nConnection: close\r\nHost: ");
	strcat(Buffer, IP);
	strcat(Buffer,"\r\nContent-Length: ");
	char Temp[16];
	sprintf(Temp,"%d", strlen(content));
	strcat(Buffer, Temp);
	strcat(Buffer,"\r\nPragma: no-cache\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; )\r\n\r\n");
	strcat(Buffer, content);

}

BOOL module_client::HttpRequest(CHAR * Send, CHAR * Receieve) {
	SOCKET ConnectSocket;
	INT RevLength;

	WSADATA wsaData;
	WSAStartup(MAKEWORD(2,2),&wsaData);

	ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  
	if (ConnectSocket == INVALID_SOCKET) {
		WSACleanup();
		return false;
	}

	if (connect(ConnectSocket,(sockaddr*) &server, sizeof(server)) == SOCKET_ERROR) {
		closesocket(ConnectSocket);
		WSACleanup();
		return false;
	}

	strcpy(Receieve, "\0");
	char Buffer[HTTP_BUFFER_LENGTH];
	send(ConnectSocket,Send,strlen(Send),0);
	memset(Buffer, 0, HTTP_BUFFER_LENGTH);

	RevLength = recv(ConnectSocket,Buffer,HTTP_BUFFER_LENGTH,0);
	if (RevLength == SOCKET_ERROR || RevLength == HTTP_BUFFER_LENGTH) {
		closesocket(ConnectSocket);
		WSACleanup();
		return FALSE;
	} else {
		CHAR * DataStartOfBuffer;
		if ((DataStartOfBuffer = strstr(Buffer, "\r\n\r\n"))!= NULL) {
			strcpy(Receieve, DataStartOfBuffer+7);
			Receieve[14] = '\0';
		}
	}
	closesocket(ConnectSocket);
	WSACleanup();
	return TRUE;
}



BOOL module_client::login(SRUNX_CLIENT_INFO * info) {
	//CHAR password[40] = "";
	//CHAR key[40];
	//
	//time_t   t = time(NULL);   
	//DWORD Temp = GetTickCount();
	//sprintf(key, "%u", (t + 4294924399) / 60);
	//CHAR Buffers[LENGTH_STRING_MAX_CONFIG*2]= {0};
	//encrypt(password, key, Buffers);

	char HttpHead[HTTP_BUFFER_LENGTH];
	char Buffer[HTTP_BUFFER_LENGTH];
	strcpy(Buffer, "username=");
	strcat(Buffer, info->username);
	strcat(Buffer, "&password=");
	strcat(Buffer, info->password);
	strcat(Buffer, "&drop=");
	strcat(Buffer, info->mode);
	strcat(Buffer, "&type=1&n=1");
	MakeHttpHead(HttpHead, "/cgi-bin/do_login", Buffer);
	if (HttpRequest(HttpHead, Buffer)) {
		if (CheckIsUID(Buffer)) {
			strcpy(UID, Buffer);
			state = 1;
			return TRUE;
		} else {
			state = 0;
			return FALSE;
		}
	}
	return TRUE;
}
BOOL module_client::logout() {
	CHAR HttpHead[HTTP_BUFFER_LENGTH];
	CHAR Buffer[HTTP_BUFFER_LENGTH];
	strcpy(Buffer, "uid=");
	strcat(Buffer, UID);
	MakeHttpHead(HttpHead, "/cgi-bin/do_logout", Buffer);
	if (HttpRequest(HttpHead, Buffer)) {
		if (strcmp(Buffer, "logout_ok") == 0) {
			state = 0;
			return TRUE;
		} else {
			state = 0;
			return FALSE;
		}
	}
	return TRUE;
}
UINT module_client::getState() {
	return state;
}

BOOL module_client::CheckIsUID(CHAR * Buffer) {
	if (strlen(Buffer) != 14) return FALSE;
	for (int i=0; i< 14; i++) {
		if (Buffer[i] > '9' || Buffer[i] < '0') {
			return FALSE;
		}
	}
	return true;
}

VOID _build_key( UINT Value, BOOL Reverse, CHAR * Buffer) {
	UINT _low = Value & 0x0f;
	UINT _high = (Value >> 4) & 0x0f;

	if (!Reverse) {
		Buffer[0] = _low  + 0x36;
		Buffer[1] = _high + 0x63;
	} else {
		Buffer[1] = _low  + 0x36;
		Buffer[0] = _high + 0x63;
	}
	return;
}

VOID module_client::encrypt(CHAR * pass, CHAR * Key, CHAR * Buffer) {
	INT len = strlen(pass);
	for (INT i= 0; i<len; i++) {
		CHAR _pass = pass[i];

		UINT Templen = strlen(Key);
		CHAR _key = Key[ Templen - i % Templen - 1 ];
		_key  = _key ^ _pass;
		_build_key(_key, i%2, Buffer + 2*i);
	}
	return;
}

